package hadoopLearn;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.io.Text;

import java.io.IOException;
import java.util.Iterator;

public class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
        throws IOException, InterruptedException
    {
         int sum = 0;
         int count = 0;
        //Iterator<IntWritable> iterator = values.iterator();
        for (IntWritable val : values) {
            sum += val.get();
            count++;
        }
        Text text = new Text("Average");
        System.out.println("Reduce: " + key.toString());
         int average = (int) sum / count;
         context.write(text, new IntWritable(average));
    }

}
